package cat.joronya.supermanager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import cat.joronya.supermanager.Supermanager.Market;
import cat.joronya.supermanager.Supermanager.MyTeam;
import cat.joronya.supermanager.Supermanager.MyTeamDetail;
import cat.joronya.supermanager.Supermanager.PrivateLeague;
import cat.joronya.supermanager.Supermanager.PrivateLeagueTeam;
import cat.joronya.supermanager.Supermanager.Home;

/**
 * This class helps open, create, and upgrade the database file.
 */
public class DatabaseHelper extends SQLiteOpenHelper 
{
	public static final String TAG = "supermanager";

	private static final int DATABASE_VERSION = 1;

	public static final String DATABASE_NAME = "Supermanager.db";
	public static final String PRIVATE_LEAGUE_TABLE_NAME = "private_leagues";
	public static final String PRIVATE_LEAGUE_TEAM_TABLE_NAME = "private_league_team";
	public static final String MARKET_TABLE_NAME = "market";
	public static final String MY_TEAM_TABLE_NAME = "my_teams";
	public static final String MY_TEAM_DETAIL_TABLE_NAME = "my_teams_detail";
	public static final String HOME_TABLE_NAME = "home";

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) 
	{
		db.execSQL("CREATE TABLE " + PRIVATE_LEAGUE_TABLE_NAME + " ("
				+ PrivateLeague._ID + " INTEGER PRIMARY KEY,"
				+ PrivateLeague.ID_PRIV + " INTEGER NOT NULL,"
				+ PrivateLeague.NAME + " TEXT NOT NULL,"
				+ PrivateLeague.NUMBER_OF_TEAMS + " TEXT NOT NULL,"
				+ PrivateLeague.MODIFIED_DATE + " INTEGER,"
				+ "UNIQUE("+PrivateLeague.ID_PRIV+")"
				+ ");");
		db.execSQL("CREATE TABLE " + PRIVATE_LEAGUE_TEAM_TABLE_NAME + " ("
                + PrivateLeagueTeam._ID + " INTEGER PRIMARY KEY,"
                + PrivateLeagueTeam._PRIVATE_LEAGUE_ID + " INTEGER NOT NULL,"
                + PrivateLeagueTeam.ID_PRIV + " INTEGER NOT NULL,"
                + PrivateLeagueTeam.NAME + " TEXT NOT NULL,"
                + PrivateLeagueTeam.DAY + " INTEGER NOT NULL,"
                + PrivateLeagueTeam.MANAGER + " TEXT NOT NULL,"
                + PrivateLeagueTeam.CITY + " TEXT NOT NULL,"
                + PrivateLeagueTeam.POINTS + " FLOAT NOT NULL,"
                + PrivateLeagueTeam.GENERAL + " FLOAT NOT NULL,"
                + PrivateLeagueTeam.BROKER + " INTEGER NOT NULL,"
                + PrivateLeagueTeam.MODIFIED_DATE + " INTEGER,"
                + "UNIQUE("+PrivateLeagueTeam.ID_PRIV+","+PrivateLeagueTeam.NAME+","+PrivateLeagueTeam.DAY+")"
                + ");");
		db.execSQL("CREATE TABLE " + MARKET_TABLE_NAME + " ("
                + Market._ID + " INTEGER PRIMARY KEY,"
                + Market.ID_PLAYER + " TEXT NOT NULL,"
                + Market.POSITION + " TEXT NOT NULL,"
                + Market.STATUS + " TEXT,"
                + Market.NACIONALITY + " TEXT,"
                + Market.NAME + " TEXT NOT NULL,"
                + Market.DAY + " INTEGER NOT NULL,"
                + Market.INFO + " TEXT,"
                + Market.TEAM + " TEXT NOT NULL,"
                + Market.BALANCE + " TEXT NOT NULL,"
                + Market.AVERAGE + " FLOAT NOT NULL,"
                + Market.PRICE + " INTEGER NOT NULL,"
                + Market.MINUTES + " TEXT NOT NULL,"
                + Market.RATING + " FLOAT NOT NULL,"
                + Market.RATING_LAST3_GAMES + " FLOAT NOT NULL,"
                + Market.INCREASE_15 + " FLOAT NOT NULL,"
                + Market.KEEP_PRICE + " FLOAT NOT NULL,"
                + Market.DECREASE_15 + " FLOAT NOT NULL,"
                + Market.MODIFIED_DATE + " INTEGER,"
                + "UNIQUE("+Market.ID_PLAYER+","+Market.DAY+")"
                + "UNIQUE("+Market.NAME+","+Market.DAY+")"
                + ");");
		db.execSQL("CREATE TABLE " + MY_TEAM_TABLE_NAME + " ("
				+ MyTeam._ID + " INTEGER PRIMARY KEY,"
				+ MyTeam.ID_EQU + " INTEGER NOT NULL,"
				+ MyTeam.DAY + " INTEGER NOT NULL,"
				+ MyTeam.NAME + " TEXT NOT NULL,"
				+ MyTeam.POINTS + " FLOAT,"
				+ MyTeam.GENERAL + " FLOAT,"
				+ MyTeam.BROKER + " INTEGER,"
				+ MyTeam.POINTS_CLASSIFICATION + " INTEGER,"
				+ MyTeam.GENERAL_CLASSIFICATION + " INTEGER,"
				+ MyTeam.BROKER_CLASSIFICATION + " INTEGER,"
				+ MyTeam.POINTS_BEST + " INTEGER,"
				+ MyTeam.GENERAL_BEST + " INTEGER,"
				+ MyTeam.POINTS_AVERAGE + " FLOAT,"
				+ MyTeam.BEST_CLASSIFICATION + " INTEGER,"
				+ MyTeam.CASH + " INTEGER,"
				+ MyTeam.EARNINGS + " INTEGER,"
				+ MyTeam.AVERAGE_SEASON + " FLOAT,"
				+ MyTeam.AVERAGE_VARIATION + " FLOAT,"
				+ MyTeam.MODIFIED_DATE + " INTEGER,"
				+ "UNIQUE("+MyTeam.ID_EQU+","+MyTeam.DAY+")"
				+ ");");
		db.execSQL("CREATE TABLE " + MY_TEAM_DETAIL_TABLE_NAME + " ("
				+ MyTeamDetail._ID + " INTEGER PRIMARY KEY,"
				+ MyTeamDetail.ID_EQU + " INTEGER NOT NULL,"
				+ MyTeamDetail.DAY + " INTEGER NOT NULL,"
				+ MyTeamDetail.POSITION + " INTEGER NOT NULL,"
                + MyTeamDetail.ID_PLAYER + " TEXT,"
				+ MyTeamDetail.COST + " INTEGER,"
				+ MyTeamDetail.SIGNING_DAY + " INTEGER,"
				+ MyTeamDetail.PRICE + " FLOAT,"
				+ MyTeamDetail.WIN + " INTEGER,"
				+ MyTeamDetail.RATING + " FLOAT,"
				+ MyTeamDetail.POINTS + " INTEGER,"
				+ MyTeamDetail.REBOUNDS + " INTEGER,"
				+ MyTeamDetail.THREE + " INTEGER,"
				+ MyTeamDetail.ASSISTS + " INTEGER,"
				+ MyTeamDetail.RATING_AVERAGE + " FLOAT,"
				+ MyTeamDetail.POINTS_AVERAGE + " FLOAT,"
				+ MyTeamDetail.REBOUNDS_AVERAGE + " FLOAT,"
				+ MyTeamDetail.THREE_AVERAGE + " FLOAT,"
				+ MyTeamDetail.ASSISTS_AVERAGE + " FLOAT,"
				+ MyTeamDetail.MODIFIED_DATE + " INTEGER,"
				+ "UNIQUE("+MyTeamDetail.ID_EQU+","+MyTeamDetail.ID_PLAYER+","+MyTeamDetail.DAY+","+MyTeamDetail.POSITION+")"
				+ ");");
		db.execSQL("CREATE TABLE " + HOME_TABLE_NAME + " ("
				+ Home._ID + " INTEGER PRIMARY KEY,"
				+ Home.DAY + " INTEGER NOT NULL,"
				+ Home.STATUS + " INTEGER,"
				+ Home.INFO + " TEXT,"
				+ Home.DUE_TIME + " TEXT,"
				+ Home.MODIFIED_DATE + " INTEGER,"
				+ "UNIQUE("+Home.DAY+")"
				+ ");");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
	{
		Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
				+ newVersion + ", which will destroy all old data");
		
		db.execSQL("DROP TABLE IF EXISTS "+PRIVATE_LEAGUE_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS "+PRIVATE_LEAGUE_TEAM_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS "+MARKET_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS" +MY_TEAM_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS" +MY_TEAM_DETAIL_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS" +HOME_TABLE_NAME);
		onCreate(db);
	}
}